<script setup>
    import { sdCityList } from '@/utils/index'
    import { getCompanyList, companyDelete } from '@/common/apis'
    import AddCompany from './AddCompany.vue'
    import { ElMessage } from 'element-plus'
    
    // 列表
    let resData = $ref([])

    // 总条数
    let totalCount = $ref(0)

    // 筛选
    let filterParams = $ref({
        pageNo: 1,
        pageSize: 20,
    })

    // 列表数据加载中
    let dataLoading = $ref(false)


    const getList = () => {
        dataLoading = true
        getCompanyList(filterParams).then(res => {
            dataLoading = false
            if(res.success){
                let { records, total } = res.result

                resData = records
                totalCount = total
            }
        })

        // setTimeout(() => {
        //     dataLoading = false
        //     let data = { s1: '济南市历城信息科技有限公司', s2: '私营企业', s3: '678866677888667KJHQ', s4: '人力资源部', s5: '刘欢', s6: '18278879987', s7: '济南市', s8: '崮云湖街道', s9: '济南市历城区大学城C区', s10: '建设银行', s11: '刘欢', s12: '6227002470150278192'  }
        //     resData = []
        //     for(let i = 0; i < filterParams.pageSize; i++){
        //         resData.push(data)
        //     }
        //     totalCount = 2540
        // }, 300);
    }
    getList()

    // 分页
    const handleCurrentChange = (page) => {
        filterParams.pageNo = page
        getList()
    }

    const handleSizeChange = (size) => {
        filterParams.pageSize = size
        getList()
    }

    // 搜索
    const hadleSearch = () => {
        filterParams.pageNo = 1
        getList()
    }

    // 重置
    const handleClickReset = () => {
        filterParams = {
            pageNo: 1,
            pageSize: 20
        }
    }

    let addCompany = $ref(null)

    // 新增
    const addSuccess = () => {
        filterParams.pageNo = 1
        getList()
    }

    // 删除
    const clickDelete = (id) => {
        companyDelete({ id }).then(res => {
            if(res.success){
                ElMessage.success('删除成功~')
                getList()
            }else{
                ElMessage.error(res.message)
            }
        })
    }

    // 停用
    const clickDisable = () => {
        ElMessage.success('操作成功~')
    }
    

</script>

<template>
    <div class="health-search">
        <div class="health-search-form-warp dz-filter-wrap">
            <el-form 
                :model="filterParams" 
                :inline="true"
                :label-width="105"
                class="mt10"
            >
                <el-form-item label="单位名称">
                    <el-input v-model="filterParams.name"></el-input>
                </el-form-item>
                <el-form-item label="单位类型">
                    <el-select v-model="filterParams.type" placeholder=" " clearable @clear="delete filterParams.type">
                        <el-option
                            v-for="(item, index) in ['用岗单位', '设岗单位']"
                            :key="index"
                            :label="item"
                            :value="item"
                        >
                            {{ item }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="行政区划">
                    <el-select v-model="filterParams.sysOrgCode" placeholder=" " clearable @clear="delete filterParams.sysOrgCode">
                        <el-option
                            v-for="(item, index) in sdCityList"
                            :key="index"
                            :label="item"
                            :value="item"
                        >
                            {{ item }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属街道">
                    <el-select v-model="filterParams.s1" placeholder=" " clearable @clear="delete filterParams.s1">
                        <el-option
                            v-for="(item, index) in ['街道1', '街道2']"
                            :key="index"
                            :label="item"
                            :value="item"
                        >
                            {{ item }}
                        </el-option>
                    </el-select>
                </el-form-item>

                <div class="btn-wrap flex-center">
                    <div class="btn reset" @click="handleClickReset">重置</div>
                    <div class="btn search" @click="hadleSearch">查询</div>
                </div>
            </el-form>

        </div>

        <!-- 查询结果 -->
        <div class="search-result mt10" v-loading="dataLoading">
            <div class="result-title flex-between">
                <div>
                    查询结果
                    <span class="result-count">共{{ totalCount }}条</span>
                </div>
                <div class="add-btn" @click="addCompany.showAddDialog({}, 'add')">新增单位</div>
            </div>
            
            <el-table 
                :data="resData" 
                style="width: 100%"
                height="570"
                size="large"
                class="dz-table"
                :scrollbar-always-on="true"
            >
                <el-table-column type="index" label="序号" min-width="60" align="center" />
                <el-table-column prop="unitName" label="单位名称" min-width="320" />
                <el-table-column prop="unitGenre" label="单位类型" min-width="120" />
                <el-table-column prop="uniformCode" label="统一社会信用代码" min-width="220" show-overflow-tooltip />
                <el-table-column prop="department" label="主管部门" min-width="160" />
                <el-table-column prop="unitPrincipal" label="单位负责人" min-width="100" align="center" />
                <el-table-column prop="phone" label="联系电话" min-width="140" align="center" />
                <el-table-column prop="administrative" label="行政区划" min-width="100" />
                <el-table-column prop="street" label="所在街道" min-width="120" />
                <el-table-column prop="unitAddress" label="单位地址" min-width="220" />
                <el-table-column prop="bank" label="开户银行" min-width="100" />
                <el-table-column prop="bankName" label="开户名" min-width="240" />
                <el-table-column prop="bankCard" label="开户账户" min-width="220" />
                <el-table-column prop="v6" label="操作" min-width="200" align="center">
                    <template #default="scope">
                        <div class="table-operation flex-center">
                            <span @click="addCompany.showAddDialog(scope.row, 'edit')">修改</span>
                            <el-popconfirm title="确定要删除此条数据吗?" @confirm="clickDelete(scope.row.id)">
                                <template #reference>
                                    <span>删除</span>
                                </template>
                            </el-popconfirm>
                            <el-popconfirm title="确定要停用此条数据吗?" @confirm="clickDisable(scope.row.id)">
                                <template #reference>
                                    <span>停用</span>
                                </template>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination 
                class="mt20" 
                background 
                layout="total, sizes, prev, pager, next" 
                :total="totalCount" 
                :page-size="filterParams.pageSize"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
            />

        </div>
    </div>

    <!-- 修改 -->
    <add-company ref="addCompany" @addSuccess="addSuccess"></add-company>

</template>
